package com.gzyj.flink.gps.map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.spring.FastJsonRedisSerializer;
import com.gzyj.flink.dto.alarm.*;
import com.gzyj.flink.gps.GPSPosition;
import com.gzyj.flink.gps.config.RedisKeyPrefixConfig;
import com.gzyj.flink.gps.config.RoadLimitConfig;
import com.gzyj.flink.gps.config.VehTypeConstants;
import com.gzyj.flink.gps.factory.MySQLMybatisSessionFactory;
import com.gzyj.flink.gps.factory.RedisSessionFactory;
import com.gzyj.flink.gps.map.roadlevel.RoadLevelLimittedFlow;
import com.gzyj.flink.gps.mapper.mysql.*;
import com.gzyj.flink.gps.service.AreaService;
import com.gzyj.flink.gps.service.YjGpsAddrService;
import com.gzyj.flink.pojo.gps.MonDaoludengjiSet;
import com.gzyj.flink.pojo.mysql.set.*;
import com.gzyj.flink.utils.PointUtils;
import com.gzyj.flink.utils.RoadUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.flink.api.common.functions.RichMapFunction;
import org.apache.flink.api.java.tuple.Tuple3;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;


import java.util.*;
import java.util.stream.Collectors;

public class RoadLevelProcess {

    private SingleOutputStreamOperator<GPSPosition> gpsSingleStream;

    public RoadLevelProcess(SingleOutputStreamOperator<GPSPosition> gpsSingleStream) {
        this.gpsSingleStream = gpsSingleStream;
    }
    public void process(){
        SingleOutputStreamOperator<Tuple3<GPSPosition, RoadLevelDataResult,MonChaosuSet> > roadlevelalarmStream= gpsSingleStream.map(new RichMapFunction<GPSPosition, Tuple3<GPSPosition, RoadLevelDataResult,MonChaosuSet>>() {

            private LettuceConnectionFactory factory;
            private SqlSessionFactory sqlSessionFactory;
            private YjGpsAddrService yjGpsAddrService;

            private boolean ApiEnableKld;

            private boolean ApiEnableBd;


            private boolean ApiEnableSwtx;


            private boolean ApiEnableMap;

            private  SqlSession session;


            private RedisTemplate<String, Object> getRedisTemplate(LettuceConnectionFactory redisSessionFactory) {
                RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
                redisTemplate.setConnectionFactory(redisSessionFactory);
                redisTemplate.setKeySerializer(new StringRedisSerializer());
                redisTemplate.setValueSerializer(new FastJsonRedisSerializer<>(Object.class));
                redisTemplate.setHashKeySerializer(new StringRedisSerializer());
                redisTemplate.setHashValueSerializer(new FastJsonRedisSerializer<>(Object.class));
                redisTemplate.afterPropertiesSet();
                return redisTemplate;
            }

            @Override
            public void open(Configuration parameters) throws Exception {
                factory = RedisSessionFactory.getRedisFactory();
                sqlSessionFactory = MySQLMybatisSessionFactory.getSqlSessionFactory();
                yjGpsAddrService=new YjGpsAddrService();
                ApiEnableKld=false;
                ApiEnableBd=true;
                ApiEnableSwtx=false;
                ApiEnableMap=true;


            }
            private static final boolean _filterFlag = true;

            private RoadLevelDataResult CompareRoadLevelData(RoadLevelDataApi apiRoadLevelData, RoadLevelDataLocal localRoadLevelData) {
                try {
                    int speed = 0;
                    if (_filterFlag) {
                        //取大
                        speed = Math.max(localRoadLevelData.getRoadMaxSpeed(), apiRoadLevelData.getRoadMaxSpeed());
                    } else {
                        //取小
                        speed = Math.min(localRoadLevelData.getRoadMaxSpeed(), apiRoadLevelData.getRoadMaxSpeed());
                        speed = speed <= 0 ? localRoadLevelData.getRoadMaxSpeed() : speed;
                    }
                    RoadLevelDataResult resultRoadLevelData = new RoadLevelDataResult();
                    resultRoadLevelData.setIsApiSource(true);
                    resultRoadLevelData.setRoadLevel(apiRoadLevelData.getRoadLevel());
                    resultRoadLevelData.setRoadMaxSpeed(speed);
                    resultRoadLevelData.setRoadNameByLevel(apiRoadLevelData.getRoadNameByLevel());
                    resultRoadLevelData.setRoadNameByPosition(apiRoadLevelData.getRoadNameByPosition());
                    return resultRoadLevelData;
                } catch (Exception ex) {

                    ex.printStackTrace();
                    return null;
                }
            }


            @Override
            public Tuple3<GPSPosition, RoadLevelDataResult,MonChaosuSet> map(GPSPosition gpsPosition) throws Exception {
                RedisTemplate<String, Object> clusterRedisTemplate = getRedisTemplate(factory);

                long len = clusterRedisTemplate.opsForList().size(RedisKeyPrefixConfig.RedisKey_VehicleLastPosition + gpsPosition.getVehId());
                List<GPSPosition> list = new ArrayList<>();
                if (len > 3) {
                    list = clusterRedisTemplate.opsForList().range(RedisKeyPrefixConfig.RedisKey_VehicleLastPosition + gpsPosition.getVehId(), 1, -1).stream().map(e -> {
                        return JSON.parseObject(JSON.toJSONString(e, SerializerFeature.DisableCircularReferenceDetect), GPSPosition.class);
                    }).collect(Collectors.toList());
                } else if (len == 3) {
                    list = clusterRedisTemplate.opsForList().range(RedisKeyPrefixConfig.RedisKey_VehicleLastPosition + gpsPosition.getVehId(), 0, -1).stream().map(e -> {
                        return JSON.parseObject(JSON.toJSONString(e, SerializerFeature.DisableCircularReferenceDetect), GPSPosition.class);
                    }).collect(Collectors.toList());
                }

                Tuple3<GPSPosition, RoadLevelDataResult,MonChaosuSet> result = null;

                if(list.size()==3){

                    session = sqlSessionFactory.openSession(true);
                    try {

                        MapVehareaMapper mapVehareaMapper= session.getMapper(MapVehareaMapper.class);
                        MapareaMapper mapareaMapper=session.getMapper(MapareaMapper.class);

                        AreaService areaService=new AreaService();
//                        RoadLevelLimittedProcess roadLevelLimittedProcess=new RoadLevelLimittedProcess(getRedisTemplate(factory),session);
                        MonChaosuSetMapper monChaosuSetMapper= session.getMapper(MonChaosuSetMapper.class);
                        TsAlarmRuleMapper tsAlarmRuleMapper= session.getMapper(TsAlarmRuleMapper.class);

                        MonChaosuSet monChaosuSet= monChaosuSetMapper.selectByVehId(gpsPosition.getVehId());

                        if(monChaosuSet==null){

                          TsAlarmRule tsAlarmRule= tsAlarmRuleMapper.selectByRuleType(gpsPosition.getShiyongxingzhi());

                          if(tsAlarmRule!=null){
                               monChaosuSet= monChaosuSetMapper.selectByVehId(tsAlarmRule.getRuleNo());
                          }

                        }

                        if(monChaosuSet!=null){
                            RoadLevelDataResult resultRoadLevelData;
                            if(monChaosuSet.getFdxsbz()==3){

                                if(monChaosuSet.getFdxs().equals(1)){

                                    int pdz = monChaosuSet.getCsxszmd();
                                    //开了分段限速则按规则取
                                    if (gpsPosition.getSpeedExt() >= pdz) {

                                        if (ApiEnableMap) {
                                            RoadLevelDataApi apiRoadLevelData = null;
                                            if (ApiEnableKld) {
                                                //调用凯立德
                                                apiRoadLevelData = new RoadLevelDataApi();
                                            } else if (ApiEnableBd) {

                                                String roadName = yjGpsAddrService.getAddress(gpsPosition.getLatExt(), gpsPosition.getLngExt());
                                                String roadLevel = RoadUtils.GetRoadLevelByRoadName(roadName);
                                                RoadLevelDataLocal roadLevelDataLocal = GetRoadLevelData(roadLevel, gpsPosition.getShiyongxingzhi(), gpsPosition.getVehId());

                                                apiRoadLevelData = new RoadLevelDataApi();
                                                apiRoadLevelData.setIsSucceed(StringUtils.isNotEmpty(roadName));
                                                apiRoadLevelData.setRoadLevel(roadLevel);
                                                apiRoadLevelData.setRoadMaxSpeed(roadLevelDataLocal.getRoadMaxSpeed());
                                                apiRoadLevelData.setRoadNameByPosition(roadName);
                                                apiRoadLevelData.setRoadNameByLevel(roadName);

                                            } else if (ApiEnableSwtx) {
                                                //调用四维图新
                                                apiRoadLevelData = new RoadLevelDataApi();
                                            }
                                            Maparea re = areaService.InChengDuArea(gpsPosition.getVehId(), gpsPosition.getLngExt(), gpsPosition.getLatExt(),mapVehareaMapper,mapareaMapper);

                                            if (re!=null) {
                                                if (apiRoadLevelData.getRoadLevel().contains("高速")) {
                                                    resultRoadLevelData = new RoadLevelDataResult();
                                                    resultRoadLevelData.setIsApiSource(true);
                                                    resultRoadLevelData.setRoadLevel(apiRoadLevelData.getRoadLevel());
                                                    resultRoadLevelData.setRoadMaxSpeed(RoadLimitConfig.InAreaHSpeed);
                                                } else {
                                                    resultRoadLevelData = new RoadLevelDataResult();
                                                    resultRoadLevelData.setIsApiSource(true);
                                                    resultRoadLevelData.setRoadLevel(apiRoadLevelData.getRoadLevel());
                                                    resultRoadLevelData.setRoadMaxSpeed(RoadLimitConfig.InAreaLSpeed);
                                                }
                                            } else {
                                                if (apiRoadLevelData.getRoadLevel().contains("高速")) {
                                                    resultRoadLevelData = new RoadLevelDataResult();
                                                    resultRoadLevelData.setIsApiSource(true);
                                                    resultRoadLevelData.setRoadLevel(apiRoadLevelData.getRoadLevel());
                                                    resultRoadLevelData.setRoadMaxSpeed(RoadLimitConfig.OutAreaHSpeed);
                                                } else {
                                                    resultRoadLevelData = new RoadLevelDataResult();
                                                    resultRoadLevelData.setIsApiSource(true);
                                                    resultRoadLevelData.setRoadLevel(apiRoadLevelData.getRoadLevel());
                                                    resultRoadLevelData.setRoadMaxSpeed(RoadLimitConfig.OutAreaLSpeed);
                                                }


                                            }
                                        } else {
                                            RoadLevelDataLocal roadLevelDataLocal = GetRoadLevelData("其他道路", gpsPosition.getShiyongxingzhi(), gpsPosition.getVehId());
                                            resultRoadLevelData = new RoadLevelDataResult();
                                            resultRoadLevelData.setIsApiSource(false);
                                            resultRoadLevelData.setRoadNameByLevel(roadLevelDataLocal.getRoadLevel());
                                            resultRoadLevelData.setRoadMaxSpeed(roadLevelDataLocal.getRoadMaxSpeed());

                                        }


                                    }else{
                                        //如果速度小于判定值，则不取接口，直接返回60的限速值，让他结束报警或不产生报警
                                        //本地配置
                                        RoadLevelDataLocal localRoadLevelData = GetRoadLevelData("其他道路", gpsPosition.getShiyongxingzhi(), gpsPosition.getVehId());
                                        resultRoadLevelData = new RoadLevelDataResult();
                                        resultRoadLevelData.setIsApiSource(false);
                                        resultRoadLevelData.setRoadLevel("其他道路");
                                        resultRoadLevelData.setRoadMaxSpeed(localRoadLevelData.getRoadMaxSpeed());
                                        resultRoadLevelData.setIgnoreYj(true);
                                    }


                                }else{
                                    //没有开启分段限速
                                    //没开分段限速则取前端默认车辆限速配置
                                    resultRoadLevelData = new RoadLevelDataResult();
                                    resultRoadLevelData.setIsApiSource(false);
                                    resultRoadLevelData.setRoadLevel("其他道路");
                                    resultRoadLevelData.setRoadMaxSpeed(monChaosuSet.getCszdsd());
                                }

//                                result= roadLevelLimittedProcess.Dispose(gpsPosition, resultRoadLevelData,monChaosuSet);
                                return new Tuple3<>(gpsPosition, resultRoadLevelData,monChaosuSet);

                            }else{
                                Maparea areaSetting= areaService.InArea(gpsPosition.getVehId(), gpsPosition.getLngExt(), gpsPosition.getLatExt(), true,mapVehareaMapper,mapareaMapper);

                                if(areaSetting!=null){
                                    resultRoadLevelData = new RoadLevelDataResult();
                                    resultRoadLevelData.setIsApiSource(false);
                                    resultRoadLevelData.setRoadLevel("其他道路");
                                    resultRoadLevelData.setRoadMaxSpeed(areaSetting.getMaxspeed().intValue());
                                    resultRoadLevelData.setRoadNameByLevel("");
                                    resultRoadLevelData.setRoadNameByPosition("");
                                    resultRoadLevelData.setSourceType(1);
                                    resultRoadLevelData.setSourceStr(String.format("区域名称：%s,区域类型：%s,区域通用限速：%s,区域类别限速：%s",
                                            areaSetting.getName(), areaSetting.getAreatype(), areaSetting.getMaxspeed(), String.join("-", Arrays.asList(areaSetting.getDkcxs() + "", areaSetting.getXkcxs() + "", areaSetting.getPhcxs() + "", areaSetting.getWhcxs() + ""))));
                                    if (areaSetting.getAreatype().equalsIgnoreCase("route")) {
                                        switch (gpsPosition.getShiyongxingzhi()) {
                                            case VehTypeConstants.DLLKYS:
                                                resultRoadLevelData.RoadMaxSpeed = areaSetting.getDkcxs().intValue();
                                                break;
                                            case VehTypeConstants.WLYYCZC:
                                            case VehTypeConstants.CZC:
                                            case VehTypeConstants.BXKYKC:
                                                resultRoadLevelData.RoadMaxSpeed = areaSetting.getXkcxs().intValue();
                                                break;
                                            case VehTypeConstants.DLHWYS:
                                                resultRoadLevelData.RoadMaxSpeed = areaSetting.getPhcxs().intValue();
                                                break;
                                            case VehTypeConstants.DLWXHWYS:
                                                resultRoadLevelData.RoadMaxSpeed = areaSetting.getWhcxs().intValue();
                                                break;
                                        }
                                    }
                                }else{
                                    //不在区域内
                                    if (monChaosuSet.getFdxs().equals(1)) {
                                        int pdz = monChaosuSet.getCsxszmd();
                                        //开了分段限速则按规则取

                                        if (gpsPosition.getSpeedExt() >= pdz) {
                                            if (ApiEnableMap) {
                                                RoadLevelDataApi apiRoadLevelData = null;
                                                if (ApiEnableKld) {
                                                    apiRoadLevelData = new RoadLevelDataApi();
                                                } else if (ApiEnableBd) {
                                                    String roadName = yjGpsAddrService.getAddress(PointUtils.gps2double(gpsPosition.getLAT()), PointUtils.gps2double(gpsPosition.getLNG()));
                                                    String roadLevel = RoadUtils.GetRoadLevelByRoadName(roadName);
                                                    RoadLevelDataLocal tp = GetRoadLevelData(roadLevel, gpsPosition.getShiyongxingzhi(), gpsPosition.getVehId());
                                                    apiRoadLevelData = new RoadLevelDataApi();
                                                    apiRoadLevelData.IsSucceed = StringUtils.isNotEmpty(roadName);
                                                    apiRoadLevelData.RoadLevel = roadLevel;
                                                    apiRoadLevelData.RoadMaxSpeed = tp.getRoadMaxSpeed();
                                                    apiRoadLevelData.RoadNameByPosition = roadName;
                                                    apiRoadLevelData.RoadNameByLevel = roadName;
                                                } else if (ApiEnableSwtx) {

                                                }
                                                //本地配置
                                                RoadLevelDataLocal localRoadLevelData = GetRoadLevelData(apiRoadLevelData.getRoadLevel(),gpsPosition.getShiyongxingzhi(), gpsPosition.getVehId());
                                                //根据规则运算 取大或取小
                                                if (monChaosuSet.getFdxsbz() == 1) {
                                                    resultRoadLevelData = CompareRoadLevelData(apiRoadLevelData, localRoadLevelData);
                                                    if (gpsPosition.getShiyongxingzhi().equalsIgnoreCase("道路危险货物运输")) {
                                                        resultRoadLevelData.setRoadMaxSpeed(resultRoadLevelData.getRoadMaxSpeed() > 80 ? 80 : resultRoadLevelData.getRoadMaxSpeed());
                                                    }
                                                    resultRoadLevelData.setSourceType(2);
                                                    resultRoadLevelData.setSourceStr("图商限速：" + resultRoadLevelData.getRoadMaxSpeed());
                                                } else {
                                                    resultRoadLevelData = new RoadLevelDataResult();
                                                    resultRoadLevelData.setIsApiSource(true);
                                                    resultRoadLevelData.setRoadLevel(localRoadLevelData.getRoadLevel());
                                                    resultRoadLevelData.setRoadMaxSpeed(localRoadLevelData.getRoadMaxSpeed());
                                                    resultRoadLevelData.setSourceType(2);
                                                    resultRoadLevelData.setSourceStr("图商限速：" + localRoadLevelData.getRoadMaxSpeed());
                                                }

                                            } else {
                                                //本地配置
                                                RoadLevelDataLocal localRoadLevelData = GetRoadLevelData("其他道路", gpsPosition.getShiyongxingzhi(), gpsPosition.getVehId());
                                                //如果没开接口开关，则不取接口
                                                resultRoadLevelData = new RoadLevelDataResult();
                                                resultRoadLevelData.setIsApiSource(false);
                                                resultRoadLevelData.setRoadLevel(localRoadLevelData.getRoadLevel());
                                                resultRoadLevelData.setRoadMaxSpeed(localRoadLevelData.getRoadMaxSpeed());
                                                resultRoadLevelData.setSourceType(2);
                                                resultRoadLevelData.setSourceStr("图商限速：" + localRoadLevelData.getRoadMaxSpeed());
                                            }
                                        } else {
                                            //如果速度小于判定值，则不取接口，直接返回60的限速值，让他结束报警或不产生报警
                                            //本地配置
                                            RoadLevelDataLocal localRoadLevelData = GetRoadLevelData("其他道路",gpsPosition.getShiyongxingzhi(), gpsPosition.getVehId());
                                            resultRoadLevelData = new RoadLevelDataResult();
                                            resultRoadLevelData.setIsApiSource(false);
                                            resultRoadLevelData.setRoadLevel("其他道路");
                                            resultRoadLevelData.setRoadMaxSpeed(localRoadLevelData.getRoadMaxSpeed());
                                            resultRoadLevelData.setIgnoreYj(true);
                                            resultRoadLevelData.setSourceType(2);
                                            resultRoadLevelData.setSourceStr("图商限速：" + localRoadLevelData.getRoadMaxSpeed());
                                        }


                                    } else {
                                        //没开分段限速则取前端默认车辆限速配置
                                        resultRoadLevelData = new RoadLevelDataResult();
                                        resultRoadLevelData.setIsApiSource(false);
                                        resultRoadLevelData.setRoadLevel("其他道路");
                                        resultRoadLevelData.setRoadMaxSpeed(monChaosuSet.getCszdsd());
                                        resultRoadLevelData.setSourceType(0);
                                        resultRoadLevelData.setSourceStr("普通限速：" + monChaosuSet.getCszdsd());
                                    }
                                }
//                                result= roadLevelLimittedProcess.Dispose(gpsPosition, resultRoadLevelData,monChaosuSet);
                                return new Tuple3<>(gpsPosition, resultRoadLevelData,monChaosuSet);

                            }


                        }


                    }catch (Exception e){
                        e.printStackTrace();
                        session.rollback();
                    }finally {
                        session.close();
                    }



                }





                return result;
            }

            @Override
            public void close() throws Exception {
                super.close();
            }

            public RoadLevelDataLocal GetRoadLevelData(String roadLevel, String vehicleType, String vehicleId) {
                try {
                    RoadLevelDataLocal localRoadLevelData = new RoadLevelDataLocal();
                    localRoadLevelData.setRoadMaxSpeed(GetVehicleSpeedByType(roadLevel, vehicleType, vehicleId));
                    localRoadLevelData.setRoadLevel(roadLevel);
                    return localRoadLevelData;
                } catch (Exception ex) {
                    return null;
                }
            }


            private int GetVehicleSpeedByType(String roadLevel, String vehicleType, String vehicleId) {
                int maxSpeed = -1;
                SqlSession sqlSession= sqlSessionFactory.openSession();
                try {
                    boolean localSource = false;

                    MonDaoludengjiSetMapper monDaoludengjiSetMapper= sqlSession.getMapper(MonDaoludengjiSetMapper.class);
                    MonDaoludengjiSet mon_daoludengji_set = monDaoludengjiSetMapper.selectByVehId(vehicleId);
                    sqlSession.commit();
                    if (mon_daoludengji_set != null) {
                        if (mon_daoludengji_set.getDelFlag().equalsIgnoreCase("1") || mon_daoludengji_set.getEnable() == 0) {
                            localSource = true;
                        } else {
                            switch (roadLevel) {
                                case "高速公路":
                                    maxSpeed = mon_daoludengji_set.getGaosu();
                                    break;
                                case "国道":
                                    maxSpeed = mon_daoludengji_set.getGuodao();
                                    break;
                                case "省道":
                                    maxSpeed = mon_daoludengji_set.getShengdao();
                                    break;
                                case "县道":
                                    maxSpeed = mon_daoludengji_set.getXiandao();
                                    break;
                                case "乡道":
                                    maxSpeed = mon_daoludengji_set.getXiangdao();
                                    break;
                                default:
                                    maxSpeed = mon_daoludengji_set.getQita();
                                    break;
                            }
                        }
                    } else {
                        localSource = true;
                    }
                    if (localSource) {
                        switch (roadLevel) {
                            case "高速公路":
                                switch (vehicleType) {
                                    case "道路旅客运输":
                                        maxSpeed = RoadLimitConfig.HighRoadMiniBus;
                                        break;
                                    case "网络预约出租车":
                                        maxSpeed = RoadLimitConfig.HighRoadBus;
                                        break;
                                    case "出租车":
                                        maxSpeed = RoadLimitConfig.HighRoadBus;
                                        break;
                                    case "道路危险货物运输":
                                        maxSpeed = RoadLimitConfig.HighRoadDangerousBus;
                                        break;
                                    case "道路货物运输":
                                        maxSpeed = RoadLimitConfig.HighRoadDangerousBusPT;
                                        break;
                                    case "班线客运客车":
                                        maxSpeed = RoadLimitConfig.HighRoadBxkc;
                                        break;
                                    default:
                                        maxSpeed = RoadLimitConfig.HighRoadOther;
                                        break;
                                }
                                break;

                            case "国道":
                                switch (vehicleType) {
                                    case "道路旅客运输":
                                        maxSpeed = RoadLimitConfig.StateRoadMiniBus;
                                        break;
                                    case "网络预约出租车":
                                        maxSpeed = RoadLimitConfig.StateRoadBus;
                                        break;
                                    case "出租车":
                                        maxSpeed = RoadLimitConfig.StateRoadBus;
                                        break;
                                    case "道路危险货物运输":
                                        maxSpeed = RoadLimitConfig.StateRoadDangerousBus;
                                        break;
                                    case "道路货物运输":
                                        maxSpeed = RoadLimitConfig.StateRoadDangerousBusPT;
                                        break;
                                    case "班线客运客车":
                                        maxSpeed = RoadLimitConfig.StateRoadBxkc;
                                        break;
                                    default:
                                        maxSpeed = RoadLimitConfig.StateRoadOther;
                                        break;
                                }
                                break;
                            case "省道":
                                switch (vehicleType) {
                                    case "道路旅客运输":
                                        maxSpeed = RoadLimitConfig.ProvincialRoadMiniBus;
                                        break;
                                    case "网络预约出租车":
                                        maxSpeed = RoadLimitConfig.ProvincialRoadBus;
                                        break;
                                    case "出租车":
                                        maxSpeed = RoadLimitConfig.ProvincialRoadBus;
                                        break;
                                    case "道路危险货物运输":
                                        maxSpeed = RoadLimitConfig.ProvincialRoadDangerousBus;
                                        break;
                                    case "道路货物运输":
                                        maxSpeed = RoadLimitConfig.ProvincialRoadDangerousBusPT;
                                        break;
                                    case "班线客运客车":
                                        maxSpeed = RoadLimitConfig.ProvincialRoadBxkc;
                                        break;
                                    default:
                                        maxSpeed = RoadLimitConfig.ProvincialRoadOther;
                                        break;
                                }
                                break;

                            case "县道":
                                switch (vehicleType) {
                                    case "道路旅客运输":
                                        maxSpeed = RoadLimitConfig.CountyRoadMiniBus;
                                        break;
                                    case "网络预约出租车":
                                        maxSpeed = RoadLimitConfig.CountyRoadBus;
                                        break;
                                    case "出租车":
                                        maxSpeed = RoadLimitConfig.CountyRoadBus;
                                        break;
                                    case "道路危险货物运输":
                                        maxSpeed = RoadLimitConfig.CountyRoadDangerousBus;
                                        break;
                                    case "道路货物运输":
                                        maxSpeed = RoadLimitConfig.CountyRoadDangerousBusPT;
                                        break;
                                    case "班线客运客车":
                                        maxSpeed = RoadLimitConfig.CountyRoadBxkc;
                                        break;
                                    default:
                                        maxSpeed = RoadLimitConfig.CountyRoadOther;
                                        break;
                                }
                                break;

                            case "乡道":
                                switch (vehicleType) {
                                    case "道路旅客运输":
                                        maxSpeed = RoadLimitConfig.TownshipRoadMiniBus;
                                        break;
                                    case "网络预约出租车":
                                        maxSpeed = RoadLimitConfig.TownshipRoadBus;
                                        break;
                                    case "出租车":
                                        maxSpeed = RoadLimitConfig.TownshipRoadBus;
                                        break;
                                    case "道路危险货物运输":
                                        maxSpeed = RoadLimitConfig.TownshipRoadDangerousBus;
                                        break;
                                    case "道路货物运输":
                                        maxSpeed = RoadLimitConfig.TownshipRoadDangerousBusPT;
                                        break;
                                    case "班线客运客车":
                                        maxSpeed = RoadLimitConfig.TownshipRoadBxkc;
                                        break;
                                    default:
                                        maxSpeed = RoadLimitConfig.TownshipRoadOther;
                                        break;
                                }
                                break;

                            default:
                                switch (vehicleType) {
                                    case "道路旅客运输":
                                        maxSpeed = RoadLimitConfig.OtherRoadMiniBus;
                                        break;
                                    case "网络预约出租车":
                                        maxSpeed = RoadLimitConfig.OtherRoadBus;
                                        break;
                                    case "出租车":
                                        maxSpeed = RoadLimitConfig.OtherRoadBus;
                                        break;
                                    case "道路危险货物运输":
                                        maxSpeed = RoadLimitConfig.OtherRoadDangerousBus;
                                        break;
                                    case "道路货物运输":
                                        maxSpeed = RoadLimitConfig.OtherRoadDangerousBusPT;
                                        break;
                                    case "班线客运客车":
                                        maxSpeed = RoadLimitConfig.OtherRoadBxkc;
                                        break;
                                    default:
                                        maxSpeed = RoadLimitConfig.OtherRoadOther;
                                        break;
                                }
                                break;
                        }
                    }


                } catch (Exception ex) {
                    sqlSession.rollback();

                }finally {
                    sqlSession.close();
                    return maxSpeed;
                }
            }


        });




        new RoadLevelLimittedFlow(roadlevelalarmStream.filter(Objects::nonNull)).process();



    }
}
